package com.md.iotest;

import java.io.*;
import java.nio.charset.StandardCharsets;
//IO - Input Output
/**
 * @Author Gong
 * @Description
 * @dATE 2022/3/28 10:25
 **/
public class IOTest {
    static String path = "d:/temp/io/";
    public static void main(String[] args) throws IOException {
        IOTest io = new IOTest();

//        String s = "我的名字是{name}";
//        s=s.replace("{name}","##");
//        System.out.println(s);
    }

    public void testFileReader() throws IOException {
        //创建 FileReader对象对象
        Reader fr = new FileReader(path+"/1.txt");
        BufferedReader br=new
                BufferedReader(fr);
        char[] buff = new char[1024];
        System.out.println(br.read());
        System.out.println(br.read());
        System.out.println(br.read());
        System.out.println(br.read());
        System.out.println(br.read());
        System.out.println(br.read());
        System.out.println(br.read());
        System.out.println(br.read());
        System.out.println(br.read());

    }




    public void copyFile(String sourcePath,String desPath) throws IOException {
        InputStream fileInputStream = new FileInputStream(sourcePath);
        FileOutputStream fout = new FileOutputStream(desPath);
        int i = -1;
        byte[] buff = new byte[1024];//1k
        while ((i=fileInputStream.read(buff))  != -1){
            //写文件
            fout.write(buff,0,buff.length);
        }
        fileInputStream.close();
        fout.close();
        System.out.println("复制完毕");
    }
    public void wrietFile() throws IOException {
        FileOutputStream fout = new FileOutputStream(path + File.separator + "2.txt");
        String str = "你好，世界";
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        fout.write(bytes,0,bytes.length);
        fout.close();
    }
    public void readFile() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(path + File.separator + "1.txt");
        int i = -1;
        byte[] buff = new byte[3];//1k
        while ((i=fileInputStream.read(buff))  != -1){
            System.out.println(new String(buff,"UTF-8"));
        }
        fileInputStream.close();
    }
    /**
     * @Author GongXL
     * @Description  创建文件目录
     * @Date 11:45 2022/3/28
     * @Param []
     * @return void
     **/
    void creatFile() throws IOException {
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();//可创不存在的多级目录
        }
        File myfile = new File(path,"1.txt");
        boolean newFile = myfile.createNewFile();
        System.out.println(newFile);
//        System.out.println(file.exists());
//        System.out.println(file.isFile());
//        System.out.println(file.isDirectory());
//        System.out.println(file.getPath());//相对于当前访问路径的文件路径
//        System.out.println(file.getAbsolutePath());//从磁盘跟开始的路径
//        System.out.println(file.getName());
//        boolean delete = file.delete();
//        System.out.println(delete);
//        boolean crfile = file.createNewFile();
//        System.out.println(crfile);
//        System.out.println(file.length());
//        file.mkdir();//存的目录下的第1级目录

    }
}
